package com.ktjiaoyu.crm.service.impl;

import com.ktjiaoyu.crm.entity.Right;
import com.ktjiaoyu.crm.entity.Role;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.RightRepository;
import com.ktjiaoyu.crm.repository.RoleRepository;
import com.ktjiaoyu.crm.repository.UserRepository;
import com.ktjiaoyu.crm.service.IUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现类---用户
 * @author Administrator
 */
@Service(value = "userService")
public class UserServiceImpl implements IUserService {
    @Resource
    private UserRepository userRepository;

    @Resource
    private RoleRepository roleRepository;

    @Resource
    private RightRepository rightRepository;

    /**
     * 登录
     *
     * @param usrName     用户名
     * @param usrPassword 密码
     * @return 用户
     */
    @Override
    public User login(String usrName, String usrPassword) {
        List<User> list = userRepository.findUserByUsrNameAndUsrPassword(usrName, usrPassword);
        if (list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 保存
     *
     * @param user 用户
     * @return 用户
     */
    @Override
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    /**
     * 删除
     *
     * @param usrId 用户ID
     */
    @Override
    public void delUser(Long usrId) {
        //获得改用户信息
        User user = userRepository.getOne(usrId);
        //删除该用户
        this.userRepository.delete(user);
    }

    /**
     * 获取用户
     *
     * @param usrId 用户id
     * @return 用户
     */
    @Override
    public User getUser(Long usrId) {
        return userRepository.getOne(usrId);
    }

    /**
     * 分页查询
     *
     * @param usrName  用户姓名
     * @param roleId   角色id
     * @param pageable 分页条件
     * @return 集合
     */
    @Override
    public Page<User> findUsers(String usrName, Long roleId, Pageable pageable) {
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (usrName!=null && !usrName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("usrName"), "%"+usrName+"%"));
                }
                if (roleId!=null && roleId.longValue()!=0l){
                    Role role = new Role();
                    role.setRoleId(roleId);
                    predicates.add(criteriaBuilder.equal(root.get("role"), role));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return userRepository.findAll(userSpecification, pageable);
    }

    /**
     * 用户名查询
     *
     * @param usrName 用户名
     * @return 用户
     */
    @Override
    public User getUserByName(String usrName) {
        return userRepository.findUserByUsrName(usrName);
    }

    /**
     * 获取所有角色
     *
     * @return 角色
     */
    @Override
    public List<Role> findAllRoles() {
        return roleRepository.findAll();
    }

    /**
     * 获取用户-->角色
     *
     * @param user
     * @return 角色
     */
    @Override
    public Role getRoleByUser(User user) {
        return roleRepository.findRoleByUsers(user);
    }

    /**
     * 保存角色
     *
     * @param role 角色
     */
    @Override
    public void saveRole(Role role) {
        roleRepository.save(role);
    }

    /**
     * 获取角色
     *
     * @param roleName 角色姓名
     * @return 角色
     */
    @Override
    public List<Role> findRoles(String roleName) {
        List<Role> list = null;
        if (roleName!=null){
            list = roleRepository.findRolesByRoleNameLike("%"+roleName+"%");
        }else {
            list = roleRepository.findAll();
        }
        return list;
    }

    /**
     * 获取所有权限
     *
     * @return 权限
     */
    @Override
    public List<Right> findAllRights() {
        return rightRepository.findAll();
    }

    /**
     * 获取用户-->角色-->权限
     *
     * @param role 角色
     * @return 权限
     */
    @Override
    public List<Right> findRightsByRole(Role role) {
        return rightRepository.findRightsByRoles(role);
    }

    /**
     * 查找所有用户
     *
     * @return 用户集合
     */
    @Override
    public List<User> findAllUsers() {
        return userRepository.findAll();
    }
}
